package com.zyf.wjy.controller;

import com.zyf.wjy.common.Result;
import com.zyf.wjy.entity.User;
import com.zyf.wjy.service.StatisticsService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * 数据统计控制器
 */
@Controller
@RequestMapping("/statistics")
public class StatisticsController {
    
    @Autowired
    private StatisticsService statisticsService;
    
    /**
     * 统计页面
     */
    @GetMapping("/index")
    public String index() {
        return "statistics/index";
    }
    
    /**
     * 数据统计仪表盘
     */
    @GetMapping("/dashboard")
    public String dashboard(HttpSession session, Model model) {
        User currentUser = (User) session.getAttribute("currentUser");
        model.addAttribute("currentUser", currentUser);
        
        // 添加各类统计数据
        model.addAttribute("costByType", statisticsService.getProductionCostByType());
        model.addAttribute("salesByMonth", statisticsService.getSalesByMonth());
        model.addAttribute("yieldByCrop", statisticsService.getYieldByCrop());
        model.addAttribute("volumeByProduct", statisticsService.getSalesVolumeByProduct());
        
        return "statistics/dashboard";
    }
    
    /**
     * 获取生产成本统计数据
     */
    @GetMapping("/cost")
    @ResponseBody
    public Result<Map<String, Double>> getProductionCost() {
        Map<String, Double> data = statisticsService.getProductionCostByType();
        return Result.success(data);
    }
    
    /**
     * 获取销售额统计数据
     */
    @GetMapping("/sales")
    @ResponseBody
    public Result<Map<String, Double>> getSales() {
        Map<String, Double> data = statisticsService.getSalesByMonth();
        return Result.success(data);
    }
    
    /**
     * 获取产量统计数据
     */
    @GetMapping("/yield")
    @ResponseBody
    public Result<Map<String, Double>> getYield() {
        Map<String, Double> data = statisticsService.getYieldByCrop();
        return Result.success(data);
    }
    
    /**
     * 获取销量统计数据
     */
    @GetMapping("/volume")
    @ResponseBody
    public Result<Map<String, Double>> getSalesVolume() {
        Map<String, Double> data = statisticsService.getSalesVolumeByProduct();
        return Result.success(data);
    }
}

